﻿using System;
using System.Collections.Generic;
using System.Linq;
using SenerPro;
using System.Web;

namespace MensUnoShop.Models
{
    public class DBPay
    {
        private DataClassesMenTradeDataContext _dcmt;
        private Logger _log = new Logger(Config.logpath);

        public int? GetCashid(string txid, int? amount, int? paytype)
        {
            int? cashid = 0;
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    _dcmt.SP_GetCashid(txid, amount, paytype, ref cashid);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetCashid", ex.ToString());
            }

            return cashid;
        }

        public PayInfo GetPayInfo(int cashid)
        {
            PayInfo pi = new PayInfo();
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    var resultCO = _dcmt.UCashOrders.SingleOrDefault(uc => uc.COcashid == cashid);
                    if (resultCO != null)
                    {
                        pi.CASHID = resultCO.COcashid;
                        pi.TXID = resultCO.COtxid;
                        pi.AMOUNT = resultCO.COamount;
                        pi.PAYTYPE = (PayType)resultCO.COpaytype;
                        pi.DATEIN = resultCO.COdatein;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetPayInfo", ex.ToString());
            }
            return pi;
        }

        public PayInfoCreditcard GetPayInfoCreditcard(int cashid)
        {
            PayInfoCreditcard pic = new PayInfoCreditcard();
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    var resultCC = _dcmt.UCreditcard.SingleOrDefault(ucc => ucc.CCcocashid == cashid);
                    if (resultCC != null)
                    {
                        pic.APPROVALCODE = resultCC.CCapprovalcode;
                        pic.BANKRESPONSECODE = resultCC.CCbankresponsecode;
                        pic.BATCHNUMBER = resultCC.CCbatchnumber;
                        pic.CARDNUMBER = resultCC.CCcardnumber;
                        pic.CHECKSUM1 = resultCC.CCchecksum1;
                        pic.CHECKSUM2 = resultCC.CCchecksum2;
                        pic.CHECKSUM3 = resultCC.CCchecksum3;
                        pic.ENGNAME = resultCC.CCengname;
                        pic.ISSUEBANK = resultCC.CCcardissuebank;
                        pic.PERIOD = resultCC.CCperiod;
                        pic.PRC = resultCC.CCprc;
                        pic.SRC = resultCC.CCsrc;
                    }
                }                
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetPayInfoCreditcard ", ex.ToString());
            }
            return pic;
        }

        public PayInfoCS GetPayInfoCS(int cashid)
        {
            PayInfoCS pics = new PayInfoCS();
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    var resultCS = _dcmt.UCS.SingleOrDefault(ucs => ucs.CScocashid == cashid);
                    if (resultCS != null)
                    {
                        pics.DUEDATE = int.Parse(resultCS.CSduedate.Value.ToString("yyyyMMdd"));
                        pics.HASH = resultCS.CShash;
                        pics.PAYNAME = resultCS.CSpayname;
                        pics.PAYPHONE = resultCS.CSpayphone;
                        pics.PAYTITLE = resultCS.CSpaytitle;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetPayInfoCS", ex.ToString());
            }
            return pics;
        }
        

        public void UptCashOrder(int? cashid, string txid, int price, PayType pt)
        {
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    _dcmt.SP_UptUCashOrders(cashid, txid, price, (int)pt);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "UptTradeData", ex.ToString());
            }
        }

        public void UptCreditcardData(int? cashid, int period, string checksum1, string checksum2, string checksum3, string prc, string src,
            string approvalcode, string bankresponsecode, string batchnumber, string cardnumber, string cardissuebank, string engname)
        {
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    _dcmt.SP_UptUCreditcard(cashid, period, checksum1, checksum2, checksum3, prc, src, approvalcode,
                        bankresponsecode, batchnumber, cardnumber, cardissuebank, engname);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "UptCreditcardData", ex.ToString());
            }
        }

        public void AddCreditcardData(int? cashid, int period, string checksum1, string checksum2, string checksum3, string prc, string src,
            string approvalcode, string bankresponsecode, string batchnumber, string cardnumber, string cardissuebank, string engname)
        {
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    _dcmt.SP_AddUCreditcard(cashid, period, checksum1, checksum2, checksum3, prc, src, approvalcode,
                        bankresponsecode, batchnumber, cardnumber, cardissuebank, engname);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "AddCreditcardData", ex.ToString());
            }
        }

        public void UptCSData(int? cashid, string paytitle, DateTime duedate, string payname, string payphone, string hash)
        {
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    _dcmt.SP_UptUCS(cashid, paytitle, duedate, payname, payphone, hash);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "UptCSData", ex.ToString());
            }
        }

        public void AddUptCSDataData(int? cashid, string paytitle, DateTime duedate, string payname, string payphone, string hash)
        {
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    _dcmt.SP_AddUCS(cashid, paytitle, duedate, payname, payphone, hash);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "AddUptCSDataData", ex.ToString());
            }
        }

        public void UptCoLog(int? cashid, string newstatus)
        {
            try
            {
                using (_dcmt = new DataClassesMenTradeDataContext(Config.connString))
                {
                    _dcmt.SP_UptUCoLog(cashid, newstatus);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "UptCoLog", ex.ToString());
            }
        }

    }
}